Node脚本

2021.1.23 星期六 21:28

向npm脚本发送命令行参数

package.json的scripts部分:

1
2
3
"scripts": {
"start": "node ./script.js server"
}

从npm2.0.0开始,可以将参数传递给npm run

1) npm scripts参数传递的命令行分割符是’–’。
比如npm run build – –name hello,即可将后续参数添加到process.env.argv数组中。

2) 在 vue-cli 创建的项目中,如果执行命令 npm run build –report ,那么参数 –report 将转化成 process.env.npm_config_report = true
3) 用shell函数,将尾巴的作为参数传进去{"build": "build(){r.js -o build.js && node version.js -v $1 && zip -r ./build/dist.zip ./build;} build"}
4) 只需在尾部提供一个$到你的脚本值条目。 `”start”: “node echoargs.js $“`
5) 如果你想将参数传递到npm脚本的中间,而不只是将它们追加到末尾,那么内联环境变量能起到一个很好的作用:

1
2
3
4
5
"scripts": {
"dev": "BABEL_ARGS=-w npm run build && cd lib/server && nodemon index.js",
"start": "npm run build && node lib/server/index.js",
"build": "mkdir -p lib && babel $BABEL_ARGS -s inline --stage 0 src -d lib",
},

在这里,npm run dev将-w watch标志传递给babel,但npm run start只运行一次常规构建。

文件指定位置写入内容

通过fs 读写文件。或者(指定)模板字符串去替换。

knowledge is no pay,reward is kindness
0%